#include "product_detail_widget.h"
#include <QPixmap>
#include <QHBoxLayout>
#include"shoppingcart_ui_logic.h"
#include"globals.h"
#include<QMessageBox>
ProductDetailWidget::ProductDetailWidget(QWidget *parent)
    : QWidget(parent)
{
    QVBoxLayout *mainLayout = new QVBoxLayout(this);

    // Setup product information
    productInfoWidget = new QWidget(this);
   // setupProductInfo();

    // Setup comments section
    commentsWidget = new QWidget(this);
    setupComments();

    // Add widgets to the main layout
    mainLayout->addWidget(productInfoWidget);
    mainLayout->addWidget(commentsWidget);

    setLayout(mainLayout);

}

void ProductDetailWidget::setupProductInfo(const QPixmap &image, const QString &theme,
                                           const QString &descriptionText, const QString &location,
                                           const QString &price, const QString date,int productId)
{
    this->setFixedSize(1680, 930);

    // Disable maximize and minimize buttons
    this->setWindowFlags(Qt::Window | Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowCloseButtonHint);
    this->productId = productId;
    QGridLayout *layout = new QGridLayout(productInfoWidget);

    // 商品图片
    imageLabel = new QLabel(productInfoWidget);
    imageLabel->setFixedSize(200, 250); // Set image size
    imageLabel->setPixmap(image.scaled(200, 250, Qt::KeepAspectRatio)); // Set the product image
    layout->addWidget(imageLabel, 0, 0, 5, 1);

    // 商品标题
    titleLabel = new QLabel(theme, productInfoWidget); // Use theme as title
    titleLabel->setStyleSheet("QLabel { font-size: 16px; font-weight: bold; }");
    titleLabel->setFixedHeight(40);
    layout->addWidget(titleLabel, 0, 1, 1, 1);

    // 商品介绍
    QLabel *descriptionLabel = new QLabel(descriptionText, productInfoWidget); // Use provided description
    descriptionLabel->setFixedHeight(40);
    layout->addWidget(descriptionLabel, 1, 1, 1, 1);

    // 商品售卖时间
    QLabel *timeLabel = new QLabel(date, productInfoWidget); // You may want to replace this with actual time
    timeLabel->setFixedHeight(40);
    layout->addWidget(timeLabel, 2, 1, 1, 1);

    // 商品产地
    QLabel *originLabel = new QLabel(location, productInfoWidget); // Use provided location
    originLabel->setFixedHeight(40);
    layout->addWidget(originLabel, 3, 1, 1, 1);

    // 商品价格
    priceLabel = new QLabel(price, productInfoWidget); // Display product price
    priceLabel->setStyleSheet("color: green; font-size: 16px; font-weight: bold;"); // Style the price
    priceLabel->setFixedHeight(40);
    layout->addWidget(priceLabel, 4, 1, 1, 1);

    // 加入购物车按钮
    cartButton = new QPushButton("加入购物车", productInfoWidget);
    cartButton->setIcon(QIcon(":/icon/light_cart.png")); // Set the cart icon
    cartButton->setStyleSheet("QPushButton { background-color: rgb(255, 165, 0); }");
    layout->addWidget(cartButton, 5, 1, 1, 1);
    connect(cartButton, &QPushButton::clicked, this, &ProductDetailWidget::onAddToCartClicked);
    productInfoWidget->setLayout(layout);
}

void ProductDetailWidget::setupProductInfo(const QPixmap &image, const QString &theme,
                                           const QString &descriptionText, const QString &price, int productId)
{
    this->setFixedSize(1680, 930);

    // Disable maximize and minimize buttons
    this->setWindowFlags(Qt::Window | Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowCloseButtonHint);
    this->productId = productId;
    QGridLayout *layout = new QGridLayout(productInfoWidget);

    // 商品图片
    imageLabel = new QLabel(productInfoWidget);
    imageLabel->setFixedSize(200, 250); // Set image size
    imageLabel->setPixmap(image.scaled(200, 250, Qt::KeepAspectRatio)); // Set the product image
    layout->addWidget(imageLabel, 0, 0, 5, 1);

    // 商品标题
    titleLabel = new QLabel(theme, productInfoWidget); // Use theme as title
    titleLabel->setStyleSheet("QLabel { font-size: 16px; font-weight: bold; }");
    titleLabel->setFixedHeight(40);
    layout->addWidget(titleLabel, 0, 1, 1, 1);

    // 商品介绍
    QLabel *descriptionLabel = new QLabel(descriptionText, productInfoWidget); // Use provided description
    descriptionLabel->setWordWrap(true); // Enable word wrap for long descriptions
    layout->addWidget(descriptionLabel, 1, 1, 1, 1);

    // 商品价格
    priceLabel = new QLabel(price, productInfoWidget); // Display product price
    priceLabel->setStyleSheet("color: green; font-size: 16px; font-weight: bold;"); // Style the price
    layout->addWidget(priceLabel, 2, 1, 1, 1);

    // 加入购物车按钮
    cartButton = new QPushButton("加入购物车", productInfoWidget);
    cartButton->setIcon(QIcon(":/icon/light_cart.png")); // Set the cart icon
    cartButton->setStyleSheet("QPushButton { background-color: rgb(255, 165, 0); }");
    layout->addWidget(cartButton, 3, 1, 1, 1);
    connect(cartButton, &QPushButton::clicked, this, &ProductDetailWidget::onAddToCartClicked);
    productInfoWidget->setLayout(layout);
}
void ProductDetailWidget::setupComments()
{

    QVBoxLayout *mainLayout = new QVBoxLayout(commentsWidget);
    QScrollArea *scrollArea = new QScrollArea(commentsWidget);
    QWidget *commentsContainer = new QWidget();
    QVBoxLayout *commentsLayout = new QVBoxLayout(commentsContainer);

    // 添加示例评论
    for (int i = 0; i < 10; ++i) {
        // 用户头像
        QLabel *avatarLabel = new QLabel(commentsWidget);
        QPixmap avatarPixmap(":/icon/light_goods.png");
        avatarPixmap = avatarPixmap.scaled(50, 50, Qt::KeepAspectRatio, Qt::SmoothTransformation);
        avatarLabel->setPixmap(avatarPixmap);
        avatarLabel->setFixedSize(50, 50);
        avatarLabel->setAlignment(Qt::AlignCenter);

        // 用户名称、评论内容、评价时间
        QVBoxLayout *infoLayout = new QVBoxLayout();
        QLabel *nameLabel = new QLabel("用户" + QString::number(i + 1), commentsWidget);
        QLabel *commentLabel = new QLabel("这是第 " + QString::number(i + 1) + " 条评论", commentsWidget);
        QLabel *timeLabel = new QLabel(QDateTime::currentDateTime().toString(), commentsWidget);
        infoLayout->addWidget(nameLabel);
        infoLayout->addWidget(commentLabel);
        infoLayout->addWidget(timeLabel);
        infoLayout->setSpacing(5);

        // 水平布局：头像 + 评论信息
        QHBoxLayout *hLayout = new QHBoxLayout();
        hLayout->addWidget(avatarLabel);
        hLayout->addLayout(infoLayout);

        QFrame *commentFrame = new QFrame(commentsWidget);
        commentFrame->setLayout(hLayout);
        commentFrame->setFrameShape(QFrame::StyledPanel);
        commentFrame->setStyleSheet("background-color: #f9f9f9; margin: 5px;");

        commentsLayout->addWidget(commentFrame);
    }

    commentsContainer->setLayout(commentsLayout);
    scrollArea->setWidget(commentsContainer);
    scrollArea->setWidgetResizable(true);
    mainLayout->addWidget(scrollArea);
    commentsWidget->setLayout(mainLayout);
}
void ProductDetailWidget::onAddToCartClicked()
{

    //ShoppingCartLogic *cartLogic =new ShoppingCartLogic(newBuyCart);
    if (ShoppingCartLogic::instance) {
        ShoppingCartLogic::resetInstance();
    }
    ShoppingCartLogic* cartLogic = ShoppingCartLogic::getInstance();
    // Pass image, theme (name), price, productId, and default quantity to cart
    cartLogic->addProductRow(imageLabel->pixmap().copy(), titleLabel->text(), priceLabel->text(), productId, 1);
    QMessageBox::information(this, "加入购物车", "商品已成功加入购物车！");

}
